package priv.mill.config;

import java.nio.charset.Charset;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@Configuration
@EnableScheduling
@PropertySource("classpath:mill.properties")
public class GlobalConfig implements SchedulingConfigurer{

	public static void main(String[] args) {
//		Yaml yaml=new Yaml();
//		InputStream resourceAsStream = GlobalConfig.class.getClassLoader().getResourceAsStream("bootstrap.yml");
//		Map<String,Object> load = yaml.load(resourceAsStream);
//		System.out.println(load);
	}

	@Bean
	public HttpMessageConverters fastjsonHttpMessageConverter() {
		
		// 定义一个转换消息的对象
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		// 添加fastjson的配置信息 比如 ：是否要格式化返回的json数据
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		fastJsonConfig.setCharset(Charset.forName("utf-8"));
		// 在转换器中添加配置信息
		fastConverter.setFastJsonConfig(fastJsonConfig);
		HttpMessageConverter<?> converter = fastConverter;
		return new HttpMessageConverters(converter);

	}
	@Override
	public void configureTasks(ScheduledTaskRegistrar arg0) {
		arg0.setScheduler(setTaskExecutors());
	}

	@Bean(destroyMethod="shutdown")
	public Executor setTaskExecutors(){
	    return Executors.newScheduledThreadPool(3); // 3个线程来处理。
	}

	
}
